package com.kou.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.kou.pojo.User;
import com.kou.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author lixu
 */
public class UserDaoImpl implements UserDao {

    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        //1：连接数据库  一个连接
        DruidDataSource dataSource = DataSourceUtil.dataSource;
        //2:Apache公司的
        QueryRunner queryRunner = new QueryRunner(dataSource);

        //3:根据用户名查询用户对象
        String sql = "select * from tb_user where username = ? ";
        //4:执行SQL
        try {
            return queryRunner.query(sql,new BeanHandler<User>(User.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 注册
     * @param user
     */
    @Override
    public void register(User user) {
        //1：连接数据库  一个连接
        DruidDataSource dataSource = DataSourceUtil.dataSource;
        //2:Apache公司的
        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql = "insert into tb_user (username,password,email,gender,status,role,code) values (?,?,?,?,?,?,?)";

        try {
            //保存
            queryRunner.update(sql,
                    user.getUsername(),
                    user.getPassword(),
                    user.getEmail(),
                    user.getGender(),
                    user.getStatus(),
                    user.getRole(),
                    user.getCode()
                    );
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 激活
     * @param email
     * @param code
     */
    @Override
    public void activate(String email, String code) {
        //1：连接数据库  一个连接
        DruidDataSource dataSource = DataSourceUtil.dataSource;
        //2:Apache公司的
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //3:SQL
        String sql = "update tb_user set status = 1 where code = ? ";
        try {
            queryRunner.update(sql,code);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public User login(String username, String password) {
        //1：连接数据库  一个连接
        DruidDataSource dataSource = DataSourceUtil.dataSource;
        //2:Apache公司的
        QueryRunner queryRunner = new QueryRunner(dataSource);

        //3:根据用户名查询用户对象
        String sql = "select * from tb_user where username = ? and password = ?";
        //4:执行SQL
        try {
            return queryRunner.query(sql,new BeanHandler<User>(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String findUserName(int uid) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select username from tb_user where id = ?";
        try {
            return (String) queryRunner.query(connection,sql,new ScalarHandler(),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
